<?php

namespace App\Models\Common\Channel;

use App\Models\Payment\ChannelPayment;
use App\Models\Payment\Payment;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;

/**
 * App\Models\Common\Channel\Channel
 *
 * @property int                      $channel_id 渠道id
 * @property string                   $name       渠道名称
 * @property string                   $type       渠道类型 1 小程序,2 APP ..
 * @property string                   $appid      渠道appid , 可以是小程序appid 等等,未录入提示未授权
 * @property string                   $secret     秘钥
 * @property string                   $app_key    客户端加密秘钥
 * @property string                   $iv         16位
 * @property Carbon|null              $created_at
 * @property Carbon|null              $updated_at
 * @property-read ChannelPayment|null $channel_payment
 * @property-read Payment|null        $default_payment
 * @method static Builder|Channel newModelQuery()
 * @method static Builder|Channel newQuery()
 * @method static Builder|Channel query()
 * @method static Builder|Channel whereAppKey($value)
 * @method static Builder|Channel whereAppid($value)
 * @method static Builder|Channel whereChannelId($value)
 * @method static Builder|Channel whereCreatedAt($value)
 * @method static Builder|Channel whereIv($value)
 * @method static Builder|Channel whereName($value)
 * @method static Builder|Channel whereSecret($value)
 * @method static Builder|Channel whereType($value)
 * @method static Builder|Channel whereUpdatedAt($value)
 * @mixin Eloquent
 */
class Channel extends Model
{
    use HasFactory;

    public    $primaryKey = 'channel_id';
    protected $table      = 'channels';
    protected $fillable
                          = [
            'name',
            'type',
            'appid',
            'secret',
            'app_key',
            'iv',
        ];

    public function getAppId(): string
    {
        return $this->getAttribute('appid');
    }

    public function getChannelId(): int
    {
        return $this->getAttribute('channel_id');
    }


    public function getSecret(): string
    {
        return $this->getAttribute('secret');
    }

    /**
     * 支付
     *
     */
    public function channel_payment(): HasOne
    {
        return $this->hasOne(ChannelPayment::class, 'appid', 'appid');
    }


    /**
     * 公众号
     *
     */
    public function wechat_office(): HasOne
    {
        return $this->hasOne(ChannelWechatOffice::class, 'appid', 'appid');
    }

    public function default_payment()
    {
        return $this->hasOneThrough(Payment::class, ChannelPayment::class, 'appid', 'mchid', 'appid', 'mchid');
    }
}
